<!-- main content -->
<div class="container">
    <div class="row-fluid">
        <div class="span12">
            <div class="w-box">
                <div class="w-box-header">
                    <h4>Менеджер номеров</h4>
                </div>
                <div class="w-box-content cnt_a user_profile">
                    <div class="row-fluid">
                        <div class="span6">
                            <select id="searchable" multiple="multiple">
                                <?php foreach ($user->getActiveSims() as $sim): ?>
                                    <option value="<?php echo $sim->getId() ?>">
                                        <?php echo $sim->getSim()->getNumber() ?>                                        
                                    </option>
                                <?php endforeach ?>
                            </select>
                            <div class="formSep sepH_b">
                                <button id="start_transfer" class="btn btn-beoro-3" type="button">Передать</button>
                                <a href="javascript:void(0)" id="deselect-all" class="btn btn-beoro-3" type="button">Отменить</a>                                
                            </div>
                        </div>
                        <div class="span3">
                            <div class="ms-container" style="background: none">
                                <div class="custom-header">Список агентов</div>
                            </div>
                            <div class="w-box-content todo-list">
                                <ul class="connectedSortable">
                                    <?php foreach ($user->getChildren() as $agent): ?>
                                        <li class="low-pr"><input type="radio" name="user" id="<?php echo $agent->getId() ?>" class="todo-check"/>
                                            <?php //jbdump($agent, 0, 'myLoveVariable'); ?>
                                            <?php echo $agent->getName() ?>
                                            <?php echo '&nbsp;&nbsp;&nbsp;&nbsp;[-' . $agent->getFullName() . '-]' ?>                                            
                                        </li>
                                    <?php endforeach ?>                                    
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="footer_space"></div>
    </div>
    <!-- jQuery form -->
    <script type="text/javascript">
        jQuery(document).ready(function ($) {
            if ($('#searchable').length) {
                //* searchable
                $('#searchable').multiSelect({
                    selectableHeader: '<div class="custom-header label-success">Свои номера</div><div class="search-header"><input type="text" class="span12" id="ms-search" autocomplete="off" placeholder="Быстрый поиск"></div>',
                    selectedHeader: "<div class='custom-header'>Передаваемые номера</div><div class='search-selected'></div>"
                });
            }
            if ($('#ms-search').length) {
                $('#ms-search').quicksearch($('li', '#ms-searchable')).on('keydown', function (e) {
                    if (e.keyCode == 40) {
                        $(this).trigger('focusout');
                        $('#ms-searchable').focus();
                        return false;
                    }
                })
            }
            $(".connectedSortable li").click(function () {
                var selected = $(this);
                $(".connectedSortable li").removeClass().addClass('high-pr');
                selected.removeClass().addClass('low-pr');
                selected.children().prop('checked', true);
            });
            $('#deselect-all').click(function(){
                $('#searchable').multiSelect('deselect_all');
                    
            });
            $("#start_transfer").click(function (e) {
                e.preventDefault();
                if ($('.ms-elem-selected').length<1) {
                    $.sticky('Выберите хотябы один номер', {autoclose: 3000, position: "top-center", type: "st-error"});
                    return false;
                }                
                if ($('input:radio:checked').length<1) {
                    $.sticky('Выберите пользователя которому хотите передать номера', {autoclose: 3000, position: "top-center", type: "st-error"});
                    return false;
                }                
                bootbox.confirm("Начать передачу номеров?", function (confirmed) {
                    if (confirmed == true) {                        
                        var user_id = $(".low-pr input").attr('id');
                        
                        var id=[];
                        $('li.ms-elem-selected').each(function (){
                            id = $(this).attr('ms-value')+','+id;
                            // compare id to what you want                            
                        });
                        id=id.slice(0, -1);
                        var send_data={"numbers": id, "user_id": user_id};
                        
                        console.log(send_data);
                        $.ajax({
                            url: "ajax/simManager.php",
                            dataType: "json",
                            type: "POST",
                            data: {transfer: send_data},
                            success: function (data) {
                                $.sticky(data.message, {autoclose: 3000, position: "top-center", type: data.type});
                                setTimeout('window.location.reload()', 2000);                                
                            },
                            error: function (data) {
                                $.sticky('Ошибка!', {autoclose: 3000, position: "top-center", type: "st-error"});
                            }
                        });
                    } else {
                        return false;
                    }
                });
            });
            
        });
    </script>